var app = getApp()
Page({
  data: {
    userInfo: {},
    truename: "",
    plate: "",
    driver_id: "",
    vehicle_id: "",
    vehicle_owner: "",
    vehicle_items: [],
    change_id:"",
    change_name: ""
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--绑定车辆" });
    var that = this;
    wx.showToast({
      title: "数据加载中，请稍候...",
      icon: 'loading',
      duration: 3000
    });
    that.setData({
      driver_id: e.driver,
      vehicle_id: e.vehicle
    })
    app.getUserInfo(function (logininfo) {
      //更新数据
      that.setData({
        userInfo: app.globalData.userInfo,
        truename: app.globalData.truename
      });
    })
    wx.request({
      url: app.appName +'/wechat/wxapplet/getVehicleData',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        openid: app.globalData.openid
      },
      success: function (res) {
        console.log(res.data.data)
        wx.hideToast();
        if (res.data.success) {
          var driver_data = res.data.data[0][0], vehicle_data = res.data.data[1];
          var vehicle_items = [];
          for (var i = 0; i < vehicle_data.length;i++){
            vehicle_items.push({ value: vehicle_data[i].ID, name: vehicle_data[i].plate })
          };
          that.setData({
            plate: driver_data.plate||"尚未绑定",
            //driver_id: driver_data.ID,
            //vehicle_id: driver_data.V_ID,
            change_id: driver_data.V_ID,
            vehicle_owner: (driver_data.vehicle_owner == driver_data.driver ? true : false),
            vehicle_items: vehicle_items
          }); console.log(that.data)
        };
      },
      fail: function (error) {
        wx.hideToast();
        console.log(error);
        that.setData({
          hasData: false
        })
      }
    })
  },
  radioChange: function (e) {
    this.setData({
      change_id: e.detail.value,
      change_name: e.detail.name
    })
  },
  submitChange:function(e){
    var that = this;
    if (that.data.change_id == that.data.vehicle_id){
      wx.showToast({ title: '您选择车辆和目前车辆一样', image: '/images/wrong.svg', duration: 2000 });
      return false;
    }
    wx.request({
      url: app.appName +'/wechat/wxapplet/changeVehicle',
      method: 'PUT',
      header: { 'content-type': 'application/json',
      'Cookie':app.globalData.cookie },
      data: { driver: that.data.driver_id, vehicle: that.data.change_id },
      success: function (res) {
        if (res.data.success) {
          wx.showToast({
            title: '已绑定车辆', icon: 'success', duration: 2000, success: function () {
              wx.redirectTo({
                url: '/pages/driver/driverInfo'
              })
            }
          });
        } else {
          wx.showToast({ title: '更换车辆失败', image: '/images/wrong.svg', duration: 2000 });
        }
      },
      fail: function (error) { wx.showToast({ title: '更换车辆失败', image: '/images/wrong.svg', duration: 2000 }); }
    });
  }
})
